"""
电机驱动文件
"""

from machine import Pin, PWM
from .pin_assignments_ import pin_assignments

motor_pins = pin_assignments.get_motor_pins()

pin_motor_sw1 = Pin(motor_pins['sw1'], Pin.OUT)
pin_motor_sw2 = Pin(motor_pins['sw2'], Pin.OUT)
pin_motor_pwm = Pin(motor_pins['pwm'], Pin.OUT)

pwm_a = PWM(pin_motor_pwm)
pwm_a.freq(1000)
pwm_a.duty(0)
pin_motor_sw1.value(0)
pin_motor_sw2.value(1)


def set_speed(speed: int):
    if -1023 <= speed <= 1023:
        if speed > 0:
            pin_motor_sw1.value(1)
            pin_motor_sw2.value(0)
        else:
            pin_motor_sw1.value(0)
            pin_motor_sw2.value(1)
        pwm_a.duty(abs(speed))
        return True
    else:
        return False
